<template>
    <button @click="handleClick" :class="className">
        <slot name="icon"></slot>
        <span>
            <slot>按钮</slot>
        </span>
    </button>
</template>


<script>
import { verifiInArray } from "../until.js";
export default {
    name: "h-button",
    props: {
        type: {
            type: String,
            default: "default",
            validator: (value) => {
                let typeArr = ["default", "primary", "success", "info", "warning", "danger"];
                let flag = verifiInArray(value, typeArr);
                if (!flag) {
                    throw new Error(`type只能是${typeArr}中的一种。`);
                }
                return flag;
            }
        },
        size: {
            type: String,
            default: "normal",
            validator: (value) => {
                let sizeArr = ["mini", "small", "normal", "medium"];
                let flag = verifiInArray(value, sizeArr);
                if (!flag) {
                    throw new Error(`size${sizeArr}中的一种。`);
                }
                return flag;
            }
        },
        disabled: {
            type: Boolean,
            dafult: false
        }
    },
    computed: {
        className: function () {
            let classNmae = ["h-button"];
            classNmae.push(`h-button--${this.type}`);
            classNmae.push(`h-button--${this.size}`);
            this.disabled ? classNmae.push(`h-button--disabled`) : "";
            return classNmae;
        }
    },
    data() {
        return {};
    },
    methods: {
        handleClick() {
            if (this.disabled) return;
            this.$emit("click", { msg: "点我了" });
        }
    }
};
</script>

<style>
.h-button {
    display: inline-block;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    background: #fff;
    border: 1px solid #dcdfe6;
    color: #606266;
    -webkit-appearance: none;
    text-align: center;
    box-sizing: border-box;
    outline: none;
    margin: 0;
    transition: 0.1s;
    font-weight: 500;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
    padding: 12px 20px;
    font-size: 14px;
    border-radius: 4px;
    letter-spacing: normal;
    word-spacing: normal;
}
.h-button:hover {
    color: #409eff;
    border-color: #c6e2ff;
    background-color: #ecf5ff;
}
/* type */
.h-button.h-button--default {
    background-color: #fff;
}
/* 主要按钮 */
.h-button.h-button--primary {
    color: #fff;
    background-color: #409eff;
    border-color: #409eff;
}
.h-button.h-button--primary:focus,
.h-button.h-button--primary:hover {
    background: #66b1ff;
    border-color: #66b1ff;
    color: #fff;
}
/* 成功按钮 */
.h-button.h-button--success {
    color: #fff;
    background-color: #67c23a;
    border-color: #67c23a;
}
.h-button.h-button--success:focus,
.h-button.h-button--success:hover {
    background: #85ce61;
    border-color: #85ce61;
    color: #fff;
}
/* 信息按钮 */
.h-button.h-button--info {
    color: #fff;
    background-color: #909399;
    border-color: #909399;
}
.h-button.h-button--info:focus,
.h-button.h-button--info:hover {
    background: #a6a9ad;
    border-color: #a6a9ad;
    color: #fff;
}
/* 危险按钮 */
.h-button.h-button--danger {
    color: #fff;
    background-color: #f56c6c;
    border-color: #f56c6c;
}
.h-button.h-button--danger:focus,
.h-button.h-button--danger:hover {
    background: #f78989;
    border-color: #f78989;
    color: #fff;
}
/* 警告按钮 */
.h-button.h-button--warning {
    color: #fff;
    background-color: #e6a23c;
    border-color: #e6a23c;
}
.h-button.h-button--warning:focus,
.h-button.h-button--warning:hover {
    background: #ebb563;
    border-color: #ebb563;
    color: #fff;
}
.h-button.h-button--disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

/* size */
.h-button--medium {
    padding: 10px 20px;
    font-size: 14px;
    border-radius: 4px;
}
.h-button--small {
    padding: 9px 15px;
    font-size: 12px;
    border-radius: 3px;
}
.h-button--mini {
    padding: 7px 15px;
    font-size: 12px;
    border-radius: 3px;
}
</style>